import { useClipboard } from '@vueuse/core'
import { message } from 'ant-design-vue'

/** 复制文本 */
export async function copyText(value: string) {
  const { copied, copy, isSupported } = useClipboard()

  if (!isSupported) {
    message.warn('不支持复制到剪贴板')
    return false
  }

  await copy(value)

  return copied.value
}

/** 导出 */
export function download(res: any, filename = '') {
  const data = res.data

  if (filename === '') {
    filename = decodeURIComponent(
      res.headers['content-disposition'].split('=')[1]
    )
  }

  const blob = new Blob([data])
  const href = URL.createObjectURL(blob)
  const a = document.createElement('a')
  a.style.display = 'none'
  a.href = href
  a.download = filename
  a.click()
  URL.revokeObjectURL(a.href)
}
